﻿using System;

namespace EchoFramework
{
    /// <summary>
    /// 游戏角色对象
    /// </summary>
    public class SpriteBase : ObjectBase
    {
        /// <summary>
        /// 游戏角色对象计数ID
        /// </summary>
        private static int SPRITE_ID;

        public int ID;
        
        protected StateMachine stateMachine;

        protected override void OnStart()
        {
            base.OnStart();
            ID = SPRITE_ID++;
            SpriteManager.Instance.AddToSpiteDict(ID, this);
            InitStateMachine();
        }

        /// <summary>
        /// 初始化有限状态机
        /// </summary>
        protected virtual void InitStateMachine()
        {
            
        }

        public override void OnUpdate()
        {
            stateMachine?.OnUpdate();
        }

        public override void OnFixedUpdate()
        {
            stateMachine?.OnFixedUpdate();
        }

        protected override void OnClose()
        {
            base.OnClose();
            if (SpriteManager.Instance != null)  //在关闭游戏时，游戏角色先注销会导致报错
            {
                SpriteManager.Instance.RemoveFromSpriteDict(ID);
            }
        }
    }
}